package com.yimi.yinhepda.views.dispatch_module;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import com.pda.ym.base.BaseActivity;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.entity.bean.DispatchBackScanChildEntity;
import com.yimi.yinhepda.entity.bean.DispatchBackScanEntity;
import com.yimi.yinhepda.event.CountEvent;

import java.util.List;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 配送运单扫描信息界面
 *
 * @author 000432
 */
public class DispatchBackScanDetail extends BaseActivity implements View.OnTouchListener {
  private TextView realWeightQty, meterageWeightQty, volume, unloadCompareTotalJian, unloadCompareTotalScaned, unloadCompareTotalNotScaned;
  private EditText waybill_no;
  private ListView listview;
  private ScrollView sv_compare;
  private UnloadCompareListViewAdapter adapter;
  private TextView num;

  @Override public String getPageName() {
    return "运单扫描信息";
  }

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_back_scan_item_detail);

    DispatchBackScanEntity entity = (DispatchBackScanEntity) getIntent().getSerializableExtra("DispatchBackScanEntity");

    ((TextView) findViewById(R.id.title)).setText("运单扫描信息");
    num =  findViewById(R.id.num);

    waybill_no =  findViewById(R.id.activity_compare_waybill);
    realWeightQty =  findViewById(R.id.activity_compare_realWeightQty);
    meterageWeightQty =  findViewById(R.id.activity_compare_meterageWeightQty);
    volume =  findViewById(R.id.activity_compare_volume);
    unloadCompareTotalJian =  findViewById(R.id.unload_compare_total_jian);
    unloadCompareTotalScaned =  findViewById(R.id.unload_compare_scaned);
    unloadCompareTotalNotScaned =  findViewById(R.id.unload_compare_notScaned);
    listview =  findViewById(R.id.activity_compare_listview);
    listview.setOnTouchListener(this);
    sv_compare =  findViewById(R.id.sv_au_compare);
    sv_compare.setOnTouchListener(this);

    waybill_no.setText(entity.getWayBillNo());

    List<DispatchBackScanChildEntity> dataList = entity.getChildEntityList();
    adapter = new UnloadCompareListViewAdapter(this, dataList);
    listview.setAdapter(adapter);

    findViewById(R.id.activity_compare_back).setOnClickListener(this);

    unloadCompareTotalJian.setText(dataList.size() + "");
    unloadCompareTotalScaned.setText(dataList.size() + "");
  }

  @Override protected void onDestroy() {
    super.onDestroy();
    //取消注册事件
    EventBus.getDefault().unregister(this);
  }

  @Subscribe(threadMode = ThreadMode.MAIN) public void onMoonEvent(CountEvent countEvent) {
    num.setText(countEvent.getCount() + "");
  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      case R.id.activity_compare_back:
        finish();
        break;
      default:
        break;
    }
  }

  @Override public boolean onTouch(View v, MotionEvent event) {
    switch (v.getId()) {
      case R.id.activity_compare_listview:
        listview.requestDisallowInterceptTouchEvent(true);
        break;
      default:
        sv_compare.requestDisallowInterceptTouchEvent(true);
        break;
    }
    return false;
  }
}

class UnloadCompareListViewAdapter extends BaseAdapter {
  private Context context;
  private List<DispatchBackScanChildEntity> dataList;

  public UnloadCompareListViewAdapter(Context context, List<DispatchBackScanChildEntity> dataList) {
    this.context = context;
    this.dataList = dataList;
  }

  @Override public int getCount() {
    return null != dataList ? dataList.size() : 0;
  }

  @Override public Object getItem(int position) {
    return null;
  }

  @Override public long getItemId(int position) {
    return 0;
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    UnloadCompareHolder holder;
    if (convertView == null) {
      holder = new UnloadCompareHolder();
      convertView = LayoutInflater.from(context).inflate(R.layout.activity_compare_listviewitem, parent, false);
      holder.childWaybillno = (TextView) convertView.findViewById(R.id.compare_listitem_waybillno);
      holder.scaneState = (TextView) convertView.findViewById(R.id.compare_listitem_scanestate);
      convertView.setTag(holder);
    } else {
      holder = (UnloadCompareHolder) convertView.getTag();
    }
    holder.childWaybillno.setText(dataList.get(position).getChildWaybillNo());
    holder.scaneState.setText("已扫描");
    convertView.setBackgroundColor(context.getResources().getColor(R.color.white));
    return convertView;
  }
}

class UnloadCompareHolder {
  TextView childWaybillno;
  TextView scaneState;
}